package com.dream.system.action;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.dream.common.Common;
import com.dream.common.DateTools;
import com.dream.common.JsonString;
import com.dream.common.Msg;
import com.dream.common.QQImage;
import com.dream.common.SuperController;
import com.dream.model.SysUser;
import com.dream.model.Sys_Question;
import com.dream.model.Sys_TestPaper;
import com.dream.model.Sys_User_TestPaper;
import com.dream.system.service.ISys_TestPaper;
import com.hxtt.c.r;

/**
 * 卷和题的管理
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/edu/sys_TestPaperAction.do")
@SuppressWarnings("all")
public class Sys_TestPaperAction extends SuperController {

	@Autowired
	private ISys_TestPaper sys_TestPaperServiceImpl;

	/**
	 * 增加卷
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 * @throws Exception
	 */
	@RequestMapping(params = "method=addTestPaper")
	public void addTestPaper(Sys_TestPaper model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		String timepicker1 = request.getParameter("timepicker1");
		String timepicker2 = request.getParameter("timepicker2");
		model.setShowStartTime(model.getShowStartTime() + " " + timepicker1);
		model.setShowEndTime(model.getShowEndTime() + " " + timepicker2);
		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		model.setSysUser(sysUser);
		int temp = 0;
		if (null != model.getTable_id() && !"".equals(model.getTable_id())) {
			temp = sys_TestPaperServiceImpl.updateTestPaper(model);
		} else {
			temp = sys_TestPaperServiceImpl.addTestPaper(model);
		}

		if (temp == 1) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}

	}

	/**
	 * 查询试卷
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getTestPaper")
	public void getTestPaper(Sys_TestPaper model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		Map map = this.sys_TestPaperServiceImpl.getTestPaper(model, sysUser);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.test_Paper);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 删除试卷
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=deleteTestPaper")
	public void deleteTestPaper(Sys_TestPaper model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		int temp = this.sys_TestPaperServiceImpl.deleteTestPaper(request.getParameter("allId"));
		if (temp > 0) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}

	}

	/**
	 * 增加卷中题
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 * @throws Exception
	 */
	@RequestMapping(params = "method=addTestPaperQuestion")
	public void addTestPaperQuestion(Sys_Question model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		String juanId = request.getParameter("juanId");
		if (null == juanId || "".equals(juanId)) {
			Msg msg = this.getMsg("0", "没有发现卷Id!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}

		Sys_TestPaper st = new Sys_TestPaper();
		st.setTable_id(juanId);
		model.setSys_TestPaper(st);
		int temp = 0;
		if (null != model.getTable_id() && !"".equals(model.getTable_id())) {
			temp = sys_TestPaperServiceImpl.updateTestPaperQuestion(model);
		} else {
			temp = sys_TestPaperServiceImpl.addTestPaperQuestion(model);
		}

		if (temp == 1) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}

	}

	/**
	 * 查询试卷
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getTestPaperQuestion")
	public void getTestPaperQuestion(Sys_TestPaper model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		Map map = this.sys_TestPaperServiceImpl.getTestPaperQuestion(model);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_question);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 删除题
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=deleteQuestion")
	public void deleteQuestion(Sys_TestPaper model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		int temp = this.sys_TestPaperServiceImpl.deleteQuestion(request.getParameter("allId"));
		if (temp > 0) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}

	}

	/**
	 * 查询老师都是当前应该批改的作业
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getHomeWorkChecked")
	public void getHomeWorkChecked(Sys_TestPaper model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		if (sysUser == null) {
			response.getWriter().write("[]");
			response.getWriter().close();
			response.getWriter().flush();
		}
		Map map = this.sys_TestPaperServiceImpl.getHomeWorkChecked(sysUser);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.test_Paper);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 自动改作业
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=autoCheckTestPaper")
	public void autoCheckTestPaper(Sys_TestPaper model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		String allId = request.getParameter("allId");
		if (null == allId || "".equals(allId)) {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}

		JSONArray jsonArray = JSONArray.fromObject(allId);
		int temp = this.sys_TestPaperServiceImpl.updateAutoCheckTestPaper(jsonArray);
		if (temp > 0) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}

	}

	/**
	 * 查询一个学生，现在所有的作业。
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getHomeWorkComplet")
	public void getHomeWorkComplet(Sys_TestPaper model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		if (sysUser == null) {
			return;
		}
		Map map = this.sys_TestPaperServiceImpl.getSudentTestPaper(model, sysUser);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.test_Paper);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();
	}

	/**
	 * 查询一个学生，现在所有的卷的作业。
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getTest_PaperQuestionInfo")
	public void getTest_PaperQuestionInfo(Sys_TestPaper model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		String userId = "";
		if ("2".equals(sysUser.getUserType()) || "5".equals(sysUser.getUserType())) {
			userId = request.getParameter("userId");
			if (null == userId || "".equals(userId)) {
				response.getWriter().write("[]");
				response.getWriter().close();
				response.getWriter().flush();
				return;
			}
		} else {
			userId = sysUser.getTable_id();
		}
		SysUser sysUser1 = new SysUser();
		sysUser1.setTable_id(userId);
		Map map = this.sys_TestPaperServiceImpl.getTest_PaperQuestionInfo(model, sysUser1);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_question);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 完成一道题
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=updateQuestionValue")
	public void updateQuestionValue(Sys_Question model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");

		String value = request.getParameter("daan");
		if (null == value || "".equals(value)) {
			Msg msg = this.getMsg("1", "答案不能为空", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		String startTime = request.getParameter("startTime");
		Long start = Common.getTimeLong(startTime);
		Long end = new Date().getTime();
		int temp1 = (int) (end - start);
		model.setCompleteTime(temp1);
		int temp = this.sys_TestPaperServiceImpl.updateQuestionValue(model, sysUser, value);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		if (temp > 0) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}

	}

	/**
	 * 完成一道题,都是给这个题 打分
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=updateQuestionQValue")
	public void updateQuestionQValue(Sys_Question model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		// SysUser sysUser = (SysUser)
		// request.getSession().getAttribute("sysUser");

		String qValue = request.getParameter("qValue");
		if (null == qValue || "".equals(qValue)) {
			Msg msg = this.getMsg("0", "没有打分", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		String userId = request.getParameter("userId");
		if (null == userId || "".equals(userId)) {
			Msg msg = this.getMsg("0", "没有找到题的对应的学生", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		SysUser sysUser = new SysUser();
		sysUser.setTable_id(userId);

		int temp = this.sys_TestPaperServiceImpl.updateQuestionQValue(model, sysUser, qValue);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		if (temp > 0) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}

	}

	/**
	 * 老师完成一个卷的批改
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=updateCompletTestPaper")
	public void updateCompletTestPaper(Sys_TestPaper model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		String juanId = request.getParameter("juanId");
		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		String userId = request.getParameter("userId");
		SysUser sysUser1 = new SysUser();
		if (userId == null || "".equals(userId)) {
			sysUser1.setTable_id(sysUser.getTable_id());

		} else {
			sysUser1.setTable_id(userId);

		}
		int temp = this.sys_TestPaperServiceImpl.updateCompletTestPaper(sysUser1, juanId);
		if (temp > 0) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}

	}

	/**
	 * 老师完成一个卷的批改
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=updateTeacherCompletTestPaper")
	public void updateTeacherCompletTestPaper(Sys_TestPaper model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		String juanId = request.getParameter("juanId");
		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		String userId = request.getParameter("userId");
		SysUser sysUser1 = new SysUser();
		if (userId == null || "".equals(userId)) {
			sysUser1.setTable_id(sysUser.getTable_id());

		} else {
			sysUser1.setTable_id(userId);

		}
		int temp = this.sys_TestPaperServiceImpl.updateTeacherCompletTestPaper(sysUser1, juanId);
		if (temp > 0) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}

	}

}
